package com.waste.game.controller;


import com.waste.game.dao.VideoAllDao;
import com.waste.game.dao.VideoPlDao;
import com.waste.game.dao.VideoPlayerDao;
import com.waste.game.dao.VideoRPDao;
import com.waste.game.entity.entitiytv.*;
import com.waste.game.service.RedisService;
import com.waste.game.utils.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Slf4j
@RestController
public class VideoPlayerController {

    @Autowired
    private VideoPlayerDao videoPlayerDao;
    @Autowired
    private VideoAllDao videoAllDao;
    @Autowired
    private VideoRPDao videoRPDao;
    @Autowired
    private VideoPlDao videoPlDao;
    @Autowired
    RedisService redisService;
    @Autowired
    RedisUtil redisUtil;

    //查询视频播放猜你喜欢
    @GetMapping("/api/doLike")
    public List<VideoAllBean> getVideoDoLike() {
        return videoAllDao.getVideoDoLike(15);
    }

    //查询视频简介
    @GetMapping("/api/videoMsg/{id}")
    public List<VideoAllBean> getVideoMsg(@PathVariable(value = "id") Integer id) {
//        System.out.println(videoAllDao.getVideoMsg(id));
        return videoAllDao.getVideoMsg(id);
    }

    //查询视频集数
    @GetMapping("/api/epicList/{id}")
    public List<VideoEpicBean> getVideoEpic(@PathVariable(value = "id") Integer id) {

        return videoPlayerDao.getVideoEpic(id);
    }

    //查询视频评价
    @GetMapping("/api/videoRp/{id}")
    public List<VideoRPBean> getVideoRP(@PathVariable(value = "id") Integer id) {
        return videoRPDao.getVideoRP(id);
    }

    //查询评论
    @GetMapping("/api/videocs/{id}/{num}")
    public List<VideoPlBean> getVideoRl(@PathVariable(value = "id") Integer id, @PathVariable(value = "num") Integer num) {
        List<VideoPlBean> listId = videoPlDao.getVideoPl(num, 10, id);
        List<Integer> arrId = new ArrayList<>();

        listId.stream().forEach(videoPlBean -> {
            arrId.add(videoPlBean.getId());
        });
        List<VideoPlBean> conentEj = new ArrayList<>();
        for (Integer integer : arrId) {
            conentEj.addAll(videoPlDao.getPl2(integer));
        }
        conentEj.addAll(0, listId);
//        System.out.println(videoAllMapper.getHFNum(1));
        return conentEj;
    }

    //
    @GetMapping("/api/numcs/{id}/{num}")
    public List<Integer> getChildNum(@PathVariable(value = "id") Integer id, @PathVariable(value = "num") Integer num) {
        List<VideoPlBean> listId = videoPlDao.getVideoPl(num, 10, id);
        List<Integer> arrId = new ArrayList<>();
        List<Integer> arrId2 = new ArrayList<>();
        listId.stream().forEach(videoPlBean -> {
            arrId.add(videoPlBean.getId());
        });
        for (Integer integer : arrId) {
            arrId2.add(videoPlDao.getHFNum(integer).intValue());
        }
//        System.out.println(arrId2);
        return arrId2;
    }

    //查询评论总数
    @GetMapping("/api/PlNum/{id}")
    public long getPlNum(@PathVariable(value = "id") Integer id) {
        return videoPlDao.getPlNum(id);
    }

    //
    @GetMapping("/api/lookNow/{id}")
    public Object getLookNum(@PathVariable(value = "id") Integer id) {
//        boolean lookBoolean = redisService.exists("lookNow"+id);
//        if (lookBoolean){
////            System.out.println("缓存拿取");
//            Object obj = redisService.get("lookNow"+id);
//            return obj;
//            obj = obj+1;
//            redisService.set("lookNow"+id,lookNow,10L, TimeUnit.MINUTES);

//            return lookNow;
//        }else {
//            System.out.println("数据库拿取");
//            int lookNow = 0;
//            lookNow = lookNow+1;
//            redisService.set("lookNow"+id,lookNow,10L, TimeUnit.MINUTES);
//            return lookNow;
//        }
//    }
        long test_incr = redisUtil.getIncr("test_incr"+id);
//        if (test_incr < 100L) {
//            long test_incr2 = redisUtil.getIncr("test_incr"+id);
//            int test_incr3 = Integer.parseInt(String.valueOf(test_incr2))-1;
//            redisUtil.setIncr("test_incr"+id,test_incr3);
//            test_incr = redisUtil.getIncr("test_incr"+id);
//        }
        if (test_incr >= 9999L){
            redisUtil.setIncr("test_incr"+id,9999);
            return 9999;
        }
        System.out.println("set");
        return test_incr;
    }
    @GetMapping("/api/lookNowGet/{id}")
    public Object getLook(@PathVariable(value = "id") Integer id) {
        System.out.println("get");
        return redisService.get("test_incr"+id);
    }
    @GetMapping("/api/lookNowDel/{id}")
    public Object getLookDel(@PathVariable(value = "id") Integer id) {
        long test_incr = redisUtil.getIncr("test_incr"+id);
        int test_incr2 = Integer.parseInt(String.valueOf(test_incr))-1;
            redisUtil.setIncr("test_incr"+id,test_incr2);
            System.out.println("cs");
        return test_incr2;
    }
}
